//
//  YNManyPeopleChat.m
//  Messenger
//
//  Created by iOS on 15/3/9.
//  Copyright (c) 2015年 YN-APP-iOS. All rights reserved.
//

#import "YNManyPeopleChat.h"
#import "FMDatabase.h"
#import "FMDatabaseQueue.h"
#import "FMDatabaseAdditions.h"
#import "YNIMTool.h"
#import "YNIMFrameworkMacro.h"

@implementation YNManyPeopleChat

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    if (!value){
        if ([key isEqualToString:@"messageType"]){
            self.messageType=0;
        }else if ([key isEqualToString:@"isSendMsg"]){
            self.isSendMsg=0;
        }else if ([key isEqualToString:@"isReaded"]){
            self.isReaded=0;
        }else if ([key isEqualToString:@"isSuccess"]){
            self.isSuccess=0;
        }else if ([key isEqualToString:@"masterType"]){
            self.masterType=0;
        }else if ([key isEqualToString:@"voiceState"]){
            self.voiceState=0;
        }else if ([key isEqualToString:@"messageHeight"]){
            self.messageHeight=0;
        }else if ([key isEqualToString:@"fileState"]){
            self.fileState=0;
        }
    }
}

+ (NSString *)packetId
{
    return @"packetId";
}

+ (NSString *)fromUserNo
{
    return @"fromUserNo";
}

+ (NSString *)toUserNo
{
    return @"toUserNo";
}

+ (NSString *)fileId
{
    return @"fileId";
}

+ (NSString *)messageType
{
    return @"messageType";
}

+ (NSString *)isSendMsg
{
    return @"isSendMsg";
}

+ (NSString *)message
{
    return @"message";
}

+ (NSString *)content
{
    return @"content";
}

+ (NSString *)mTime
{
    return @"mTime";
}

+ (NSString *)isReaded
{
    return @"isReaded";
}

+ (NSString *)isSuccess
{
    return @"isSuccess";
}

+ (NSString *)groupNo
{
    return @"groupNo";
}

+ (NSString *)masterType
{
    return @"masterType";
}

+ (NSString *)sendName
{
    return @"sendName";
}

+ (NSString *)messageBody
{
    return @"messageBody";
}

+ (NSString *)messageHeight
{
    return @"messageHeight";
}

+ (NSString *)fileName
{
    return @"fileName";
}

+ (NSString *)originPhotoUrl
{
    return @"originPhotoUrl";
}

+ (NSString *)fileUrl
{
    return @"fileUrl";
}

+ (NSString *)fileState
{
    return @"fileState";
}


//-----------jc添加---------
+(NSString *)voicePath
{
    return @"voicePath";
}

+(NSString *)voiceName
{
    return @"voiceName";
}

+(NSString *)messageContinuousKey
{
    return @"messageContinuousKey";
}

+(NSString *)isMessageContinuous
{
    return @"isMessageContinuous";
}


+(NSString*)aitContactMark
{
    return @"aitContactMark";
}

+(NSString *)aitContactMarkArray
{
    return @"aitContactMarkArray";
}


+(NSString *)isMessageWithdraw
{
    return @"isMessageWithdraw";
}


//-----------jc添加---------


+ (NSString *)tableName:(int)groupType
{
    if (groupType == kContact_UserGroupType)
    {
        return @"UserGroupChat";
    }
    else if (groupType == kContact_DiscussionGropType)
    {
        return @"DiscussionGroupChat";
    }
    else
    {
        return @"ProjectTeamGroupChat";
    }
    
}


/**
 给aitContactMark设置默认值0
 
 @return
 */
-(NSInteger)aitContactMark
{
    if(_aitContactMark<=0)
    {
        _aitContactMark = 0 ;
    }
    return _aitContactMark;
}


-(NSArray *)insertParam
{
    return @[[YNIMTool nilFilter:self.packetId],
             [YNIMTool nilFilter:self.fromUserNo],
             [YNIMTool nilFilter:self.toUserNo],
             [YNIMTool nilFilter:self.fileId],
             [NSNumber numberWithInt:self.messageType],
             [NSNumber numberWithInt:self.isSendMsg],
             [YNIMTool nilFilter:self.message],
             [YNIMTool nilFilter:self.content],
             [YNIMTool nilFilter:self.mTime],
             [NSNumber numberWithInt:self.isReaded],
             [NSNumber numberWithInt:self.isSuccess],
             [YNIMTool nilFilter:self.groupNo],
             [NSNumber numberWithInt:self.masterType],
             [YNIMTool nilFilter:self.sendName],
             [YNIMTool nilFilter:self.messageBody],
             [NSNumber numberWithFloat:self.messageHeight],
             [YNIMTool nilFilter:self.fileName],
             [YNIMTool nilFilter:self.originPhotoUrl],
             [YNIMTool nilFilter:self.fileUrl],
             [NSNumber numberWithInteger:self.fileState],
             [YNIMTool nilFilter:self.messageContinuousKey],
             [NSNumber numberWithInteger:self.isMessageContinuous],
             [NSNumber numberWithInt:self.isMessageWithdraw],
             [NSNumber numberWithInteger:self.aitContactMark],
             [YNIMTool nilFilter:self.aitContactMarkArray]];
}

+ (BOOL)creatManyPeopleChatWithGroupType:(int)groupType inDatabase:(FMDatabase *)db
{
    if ([db tableExists:[YNManyPeopleChat tableName:groupType]]) {
        return YES;
    }
    
    NSString *creatManyPeopleChatSql = [NSString stringWithFormat:@"%@%@(%@ varchar(30) NOT NULL PRIMARY KEY, %@ varchar(20), %@ varchar(20), %@ varchar(100), %@ INTEGER, %@ INTEGER, %@ TEXT, %@ TEXT, %@ varchar(30), %@ INTEGER, %@ INTEGER, %@ varchar(30), %@ INTEGER, %@ varchar(30), %@ varchar(200), %@ FLOAT, %@ varchar(30), %@ varchar(30), %@ varchar(30), %@ INTEGER, %@ INTEGER, %@ INTEGER, %@ INTEGER, %@ INTEGER, %@ varchar(150))",
                                        CREATE_TABLE,
                                        [YNManyPeopleChat tableName:groupType],
                                        [YNManyPeopleChat packetId],
                                        [YNManyPeopleChat fromUserNo],
                                        [YNManyPeopleChat toUserNo],
                                        [YNManyPeopleChat fileId],
                                        [YNManyPeopleChat messageType],
                                        [YNManyPeopleChat isSendMsg],
                                        [YNManyPeopleChat message],
                                        [YNManyPeopleChat content],
                                        [YNManyPeopleChat mTime],
                                        [YNManyPeopleChat isReaded],
                                        [YNManyPeopleChat isSuccess],
                                        [YNManyPeopleChat groupNo],
                                        [YNManyPeopleChat masterType],
                                        [YNManyPeopleChat sendName],
                                        [YNManyPeopleChat messageBody],
                                        [YNManyPeopleChat messageHeight],
                                        [YNManyPeopleChat fileName],
                                        [YNManyPeopleChat originPhotoUrl],
                                        [YNManyPeopleChat fileUrl],
                                        [YNManyPeopleChat fileState],
                                        [YNManyPeopleChat messageContinuousKey],
                                        [YNManyPeopleChat isMessageContinuous],
                                        [YNManyPeopleChat isMessageWithdraw],
                                        [YNManyPeopleChat aitContactMark],
                                        [YNManyPeopleChat aitContactMarkArray]];
    
    BOOL result = [db executeUpdate:creatManyPeopleChatSql];
    return result;
}


/**JC** 在表中新增一个列（字段） **/
+(BOOL)insertManyColumnInDatabase:(FMDatabase*)db MessageContinuous:(NSString*)MessageContinuous columnDataType:(NSString*)type GroupType:(int)groupType{
    if (![db columnExists:MessageContinuous inTableWithName:[YNManyPeopleChat tableName:groupType]]) {
        //列name在表中不存在，所以加入
        BOOL result= [db executeUpdate:[NSString stringWithFormat:@"%@ %@ add %@ %@",ALTER_TABLE,[YNManyPeopleChat tableName:groupType],MessageContinuous,type]];
        return result;
    }
    return YES;
}



/**HX** 根据packId判断数据库是否存在这条消息 **/
+ (BOOL)determineGroupChatTableWithPackId:(NSString *)packId groupType:(int)groupType Database:(FMDatabase *)db
{
    NSString *queryUserNoSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ LIKE '%@'",
                                [YNManyPeopleChat tableName:groupType],
                                [YNManyPeopleChat packetId],
                                packId];
    FMResultSet *rs = [db executeQuery:queryUserNoSql];
    
    BOOL result = NO;
    while ([rs next])
    {
        result = YES;
    }
    [rs close];
    return result;
}

/**JC** 根据packId获取这条消息 **/
+(NSArray *)queryGroupChatTableWithPackId:(NSString *)packId groupType:(int)groupType Database:(FMDatabase *)db
{
    NSString *queryUserNoSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = '%@'",
                                [YNManyPeopleChat tableName:groupType],
                                [YNManyPeopleChat packetId],
                                packId];
    
    FMResultSet *rs = [db executeQuery:queryUserNoSql];
    NSMutableArray *manyPeopleChatArray = [NSMutableArray array];
    
    while ([rs next])
    {
        YNManyPeopleChat *manyPeopleChat = [[YNManyPeopleChat alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [manyPeopleChat setValuesForKeysWithDictionary:[rs resultDictionary]];
            [manyPeopleChatArray addObject:manyPeopleChat];
        }
        
    }
    [rs close];
    return manyPeopleChatArray;
}


- (BOOL)insertManyPeopleChatWithGroupType:(int)groupType inDatabase:(FMDatabase *)db
{
    BOOL isChatMessageExist =  [YNManyPeopleChat determineGroupChatTableWithPackId:self.packetId groupType:groupType Database:db];
    BOOL result=YES;
    
    //messageContinuousKey 取值为最近第一条消息的messageContinuousKey值//
    if([YNIMTool isEmptyString:self.messageContinuousKey])
    {
        NSArray * manyPeopleArray  =[YNManyPeopleChat queryManPeopleChatTopInfoWithGroupNo:self.groupNo GroupType:groupType mTime:nil numberCount:1 Database:db];
        if([YNIMTool isEmptyArray:manyPeopleArray])
        {
            //没有消息，key值取新的GUID值
            self.messageContinuousKey = [YNIMTool getGUID];
        }
        else
        {
            YNManyPeopleChat * manyPeopleChat = manyPeopleArray.firstObject;
            if([YNIMTool isEmptyString:manyPeopleChat.messageContinuousKey])
            {
                //没有消息，key值取新的GUID值
                self.messageContinuousKey = [YNIMTool getGUID];
            }
            else
            {
                self.messageContinuousKey = manyPeopleChat.messageContinuousKey;
            }
            
        }
    }
    //messageContinuousKey 取值为最近第一条消息的messageContinuousKey值//
    
    
    //不存在本条信息，直接插入
    if (!isChatMessageExist) {
        
        result = [self insertManyPeopleChatInfoWithGroupType:groupType inDatabase:db];
    }
    //存在这条消息，并且isMessageContinuous是连续YES
    //修改所有的值为messageContinuousKey，isMessageContinuous相同的为最新的key值
    else if(isChatMessageExist == YES && self.isMessageContinuous == YES)
    {
        NSArray * twoPeopleArray = [YNManyPeopleChat queryGroupChatTableWithPackId:self.packetId groupType:groupType Database:db];
        if(![YNIMTool isEmptyArray:twoPeopleArray])
        {
            YNManyPeopleChat * oldManyPeopleChat = twoPeopleArray.firstObject;
            //1,修改当前数据信息
            result= [YNManyPeopleChat updateManyPeopleMessageContinuousTableWithPackId:self.packetId groupType:groupType messageContinuousKey:self.messageContinuousKey isMessageWithdraw:self.isMessageWithdraw inDatabase:db];
            //2，修改所有的messageContinuousKey相同值，isMessageContinuous为YES的数据。
            if(![YNIMTool isEmptyString:oldManyPeopleChat.messageContinuousKey])
            {
                result = [YNManyPeopleChat updateManyPeopleAllMessageContinuousWithMessageContinuousKey:self.messageContinuousKey oldMessageContinuousKey:oldManyPeopleChat.messageContinuousKey groupType:groupType inDatabase:db];
            }
            
        }
    }
    
    return result;
    
}

-(BOOL)insertManyPeopleChatInfoWithGroupType:(int)groupType inDatabase:(FMDatabase *)db
{
    BOOL result;
    NSString *fieldString = [NSString stringWithFormat:@"%@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@",
                             [YNManyPeopleChat packetId],
                             [YNManyPeopleChat fromUserNo],
                             [YNManyPeopleChat toUserNo],
                             [YNManyPeopleChat fileId],
                             [YNManyPeopleChat messageType],
                             [YNManyPeopleChat isSendMsg],
                             [YNManyPeopleChat message],
                             [YNManyPeopleChat content],
                             [YNManyPeopleChat mTime],
                             [YNManyPeopleChat isReaded],
                             [YNManyPeopleChat isSuccess],
                             [YNManyPeopleChat groupNo],
                             [YNManyPeopleChat masterType],
                             [YNManyPeopleChat sendName],
                             [YNManyPeopleChat messageBody],
                             [YNManyPeopleChat messageHeight],
                             [YNManyPeopleChat fileName],
                             [YNManyPeopleChat originPhotoUrl],
                             [YNManyPeopleChat fileUrl],
                             [YNManyPeopleChat fileState],
                             [YNManyPeopleChat messageContinuousKey],
                             [YNManyPeopleChat isMessageContinuous],
                             [YNManyPeopleChat isMessageWithdraw],
                             [YNManyPeopleChat aitContactMark],
                             [YNManyPeopleChat aitContactMarkArray]];
    NSString *insertManyPeopleChatSql = [NSString stringWithFormat:@"%@%@ (%@) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", INSERT_TABLE, [YNManyPeopleChat tableName:groupType], fieldString];
    
    result = [db executeUpdate:insertManyPeopleChatSql withArgumentsInArray:[self insertParam]];
    
    return result;
    
}


+ (BOOL)deleteAllChatWithGroupType:(int)groupType inDatabase:(FMDatabase *)db
{
    if (![db tableExists:[YNManyPeopleChat tableName:groupType]]) {
        return YES;
    }
    NSString *deleteAllManyPeopleChatSql = [NSString stringWithFormat:@"DELETE FROM %@ ", [YNManyPeopleChat tableName:groupType]];
    
    //删除自增字段数据
    NSString *deleteAutoFieldSql = [NSString stringWithFormat:@"UPDATE sqlite_sequence SET seq = 0 WHERE name='%@'", [YNManyPeopleChat tableName:groupType]];
    
    BOOL result = [db executeUpdate:deleteAllManyPeopleChatSql];
    
    if (result)
    {
        result = [db executeUpdate:deleteAutoFieldSql];
    }
    
    return result;
}

+ (BOOL)deleteManyPeopleChatWithGroupType:(int)groupType groupNo:(NSString *)groupNo inDatabase:(FMDatabase *)db
{
    NSString *deleteTwoPeopleChatSql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = ?", [YNManyPeopleChat tableName:groupType], [YNManyPeopleChat groupNo]];
    
    BOOL result = [db executeUpdate:deleteTwoPeopleChatSql,groupNo];
    
    return result;
}

+ (NSArray *)queryManyPeopleChatWithGroupTyoe:(int)groupType groupNo:(NSString *)groupNo startIndex:(NSInteger)start endIndex:(NSInteger)end isSelectSendSuccessMessage:(BOOL)selectSuccess inDatabase:(FMDatabase *)db
{
    NSString *OrderByStr = [NSString stringWithFormat:@" ORDER BY %@ DESC",[YNManyPeopleChat mTime]];
    return [self queryManyPeopleChatWithUserNo:groupNo
                                     groupType:groupType
                                       orderBy:OrderByStr
                                    startIndex:start
                                      endIndex:end
                    isSelectSendSuccessMessage:selectSuccess
                                    inDatabase:db];
}

+ (NSArray *)queryManyPeopleChatWithUserNo:(NSString *)userNo groupType:(int)groupType orderBy:(NSString *)orderBy startIndex:(NSInteger)start endIndex:(NSInteger)end isSelectSendSuccessMessage:(BOOL)selectSuccess inDatabase:(FMDatabase *)db
{
    NSString *queryManyPeopleChatSql;
    if (selectSuccess)
    {
        queryManyPeopleChatSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE 1 = 1 AND %@ = '%@' AND %@ = %@ %@ LIMIT %ld, %ld",
                                  [YNManyPeopleChat tableName:groupType],
                                  [YNManyPeopleChat groupNo],
                                  userNo,
                                  [YNManyPeopleChat isSuccess],
                                  [NSNumber numberWithBool:selectSuccess],
                                  orderBy,
                                  (long)start,
                                  (long)end];
    }
    else
    {
        queryManyPeopleChatSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE 1 = 1 AND %@ = '%@' %@ LIMIT %ld, %ld",
                                  [YNManyPeopleChat tableName:groupType],
                                  [YNManyPeopleChat groupNo],
                                  userNo,
                                  orderBy,
                                  (long)start,
                                  (long)end];
    }
    
    NSMutableArray *manyPeopleChatArray = [NSMutableArray array];
    
    FMResultSet *rs = [db executeQuery:queryManyPeopleChatSql];
    
    while ([rs next])
    {
        YNManyPeopleChat *manyPeopleChat = [[YNManyPeopleChat alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [manyPeopleChat setValuesForKeysWithDictionary:[rs resultDictionary]];
        }
        
        [manyPeopleChatArray addObject:manyPeopleChat];
    }
    [rs close];
    
    return manyPeopleChatArray;
}

/**
 查询多人对话未读消息的第一条消息
 
 @param groupNo 当前聊天所在的讨论组/项目组或群的ID
 @param groupType 类型
 @param unReadCount 未读条数
 @param db db
 @return 返回数据
 */
+ (NSArray *)queryUnReadFirstManyPeopleChatGroupNo:(NSString *)groupNo groupType:(int)groupType unReadCount:(NSInteger)unReadCount  inDatabase:(FMDatabase *)db
{
    NSString *OrderByStr = [NSString stringWithFormat:@" ORDER BY %@ DESC",[YNManyPeopleChat mTime]];
    NSString * queryManyPeopleChatSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = '%@' %@ LIMIT 0, %ld",
                                         [YNManyPeopleChat tableName:groupType],
                                         [YNManyPeopleChat groupNo],
                                         groupNo,
                                         OrderByStr,unReadCount];
    
    NSMutableArray *manyPeopleChatArray = [NSMutableArray array];
    
    FMResultSet *rs = [db executeQuery:queryManyPeopleChatSql];
    
    while ([rs next])
    {
        YNManyPeopleChat *manyPeopleChat = [[YNManyPeopleChat alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [manyPeopleChat setValuesForKeysWithDictionary:[rs resultDictionary]];
        }
        [manyPeopleChatArray addObject:manyPeopleChat];
        
    }
    [rs close];
    
    return manyPeopleChatArray;
}

/**
 查询艾特@数据
 2018-11-16 JC添加
 @param groupType
 @param groupNo
 @param db db
 @return 返回结果
 */
+ (NSArray *)queryManyPeopleChatAitWithGroupTyoe:(int)groupType groupNo:(NSString *)groupNo inDatabase:(FMDatabase *)db
{
    NSArray * returnArray ;
    
    //查询艾特@自己的数据
    returnArray= [YNManyPeopleChat queryManyPeopleChatAitWithGroupTyoe:groupType groupNo:groupNo aitContactMark:1 inDatabase:db];
    
    //查询艾特全部人员
    if([YNIMTool isEmptyArray:returnArray])
    {
        returnArray = [YNManyPeopleChat queryManyPeopleChatAitWithGroupTyoe:groupType groupNo:groupNo aitContactMark:2 inDatabase:db];
    }
    
    return returnArray;
    
}


/**
 查询艾特个人或艾特所有人
 
 @param groupType
 @param groupNo
 @param aitContactMark 1：艾特@自己，2：艾特所有人
 @param db db
 @return 返回结果
 */
+ (NSArray *)queryManyPeopleChatAitWithGroupTyoe:(int)groupType groupNo:(NSString *)groupNo aitContactMark:(NSInteger)aitContactMark inDatabase:(FMDatabase *)db
{
    
    NSString *OrderByStr = [NSString stringWithFormat:@" ORDER BY %@ DESC",[YNManyPeopleChat mTime]];
    NSString * queryManyPeopleChatSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = '%@' AND %@ = %ld %@ ",
                                         [YNManyPeopleChat tableName:groupType],
                                         [YNManyPeopleChat groupNo],
                                         groupNo,
                                         [YNManyPeopleChat aitContactMark],
                                         aitContactMark,
                                         OrderByStr];
    
    
    NSMutableArray *manyPeopleChatArray = [NSMutableArray array];
    
    FMResultSet *rs = [db executeQuery:queryManyPeopleChatSql];
    
    while ([rs next])
    {
        YNManyPeopleChat *manyPeopleChat = [[YNManyPeopleChat alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [manyPeopleChat setValuesForKeysWithDictionary:[rs resultDictionary]];
        }
        [manyPeopleChatArray addObject:manyPeopleChat];
        
    }
    [rs close];
    
    return manyPeopleChatArray;
    
    
}


/**
 查询艾特@个人或艾特@所有人，离最新一条数据共有多少条数据
 2018-11-16 JC添加
 
 @param groupType
 @param groupNo
 @param aitContactMark 1：艾特@自己，2：艾特所有人
 @param db db
 @return 返回结果
 */
+ (NSInteger)queryManyPeopleChatAitCountWithGroupTyoe:(int)groupType groupNo:(NSString *)groupNo ynmanyPeopleChat:(YNManyPeopleChat*)ynmanyPeopleChat inDatabase:(FMDatabase *)db
{
    
    NSString *OrderByStr = [NSString stringWithFormat:@" ORDER BY %@ DESC",[YNManyPeopleChat mTime]];
    NSString * queryManyPeopleChatSql = [NSString stringWithFormat:@"SELECT COUNT(*) FROM %@ WHERE %@ = '%@' AND (%@ > datetime('%@') OR %@ = '%@') %@ ",
                                         [YNManyPeopleChat tableName:groupType],
                                         [YNManyPeopleChat groupNo],
                                         groupNo,
                                         [YNManyPeopleChat mTime],
                                         ynmanyPeopleChat.mTime,
                                         [YNManyPeopleChat mTime],
                                         ynmanyPeopleChat.mTime,
                                         OrderByStr];
    
    NSInteger manyPeopleCount = [db intForQuery:queryManyPeopleChatSql];
    
    return manyPeopleCount;
}


/**
 根据groupNo查询对应该条信息
 2019-1-30 JC添加
 @param groupType
 @param groupNo
 @param db db
 @return 返回结果
 */
+ (NSArray *)queryManyPeopleChatWithGroupTyoe:(int)groupType groupNo:(NSString *)groupNo inDatabase:(FMDatabase *)db
{
    NSString *queryTwoPeopleChatByKeywordSql = [NSString stringWithFormat:@"SELECT * FROM  %@ WHERE %@ = '%@'",[YNManyPeopleChat tableName:groupType],[YNManyPeopleChat groupNo],groupNo];
    
    FMResultSet *rs = [db executeQuery:queryTwoPeopleChatByKeywordSql];
    NSMutableArray *twoPeopleChatByKeywordArray = [NSMutableArray array];
    while ([rs next])
    {
        YNManyPeopleChat * manyPeopleChat = [[YNManyPeopleChat alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [manyPeopleChat setValuesForKeysWithDictionary:[rs resultDictionary]];
            [twoPeopleChatByKeywordArray addObject:manyPeopleChat];
        }
    }
    [rs close];
    
    return twoPeopleChatByKeywordArray;
}

+ (NSArray *)queryManyPeopleChatWithGroupType:(int)groupType ByKeywords:(NSString *)keyWords groupNo:(NSString *)groupNo inDatabase:(FMDatabase *)db
{
    NSString *queryManyPeopleChatByKeywordSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ LIKE '%%%@%%'escape '/' AND %@ = ? AND %@ = 0 ORDER BY %@ DESC",
                                                 [YNManyPeopleChat tableName:groupType],
                                                 [YNManyPeopleChat content],
                                                 keyWords,
                                                 [YNManyPeopleChat groupNo],
                                                 [YNManyPeopleChat isMessageWithdraw],
                                                 [YNManyPeopleChat mTime]];
    
    FMResultSet *rs = [db executeQuery:queryManyPeopleChatByKeywordSql,groupNo];
    NSMutableArray *manyPeopleChatByKeywordArray = [NSMutableArray array];
    
    while ([rs next])
    {
        YNManyPeopleChat * manyPeopleChat = [[YNManyPeopleChat alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [manyPeopleChat setValuesForKeysWithDictionary:[rs resultDictionary]];
        }
        
        
        [manyPeopleChatByKeywordArray addObject:manyPeopleChat];
    }
    
    [rs close];
    
    return manyPeopleChatByKeywordArray;
    
}

/**
 用于消息漫游判断
 查询最近多少条信息
 
 @param groupNo 讨论组、群、组织机构id
 @param groupType 讨论组、群、组织机构类型
 @param sendTime 保存时间
 @param numberCount 多少条数据
 @param db db
 @return 返回结果
 */
+(NSArray *)queryManPeopleChatTopInfoWithGroupNo:(NSString *)groupNo GroupType:(int)groupType mTime:(NSString *)mTime numberCount:(NSInteger)numberCount Database:(FMDatabase *)db
{
    NSString *queryTwoPeopleChatByKeywordSql;
    //sendTime为空，获取最近numberCount条数据
    if([YNIMTool isEmptyString:mTime])
    {
        queryTwoPeopleChatByKeywordSql = [NSString stringWithFormat:@"SELECT * FROM  %@ WHERE %@ = '%@' ORDER BY %@ DESC limit %ld",[YNManyPeopleChat tableName:groupType],[YNManyPeopleChat groupNo],groupNo,[YNManyPeopleChat mTime],numberCount];
    }
    //sendTime有值，获取sendTime时间之前numberCount条数据
    else
    {
        
        queryTwoPeopleChatByKeywordSql = [NSString stringWithFormat:@"SELECT * FROM  %@ WHERE %@ = '%@' AND %@ < datetime('%@') ORDER BY %@ DESC limit %ld",[YNManyPeopleChat tableName:groupType],[YNManyPeopleChat groupNo],groupNo,[YNManyPeopleChat mTime],mTime,[YNManyPeopleChat mTime],numberCount];
        
    }
    
    FMResultSet *rs = [db executeQuery:queryTwoPeopleChatByKeywordSql];
    NSMutableArray *twoPeopleChatByKeywordArray = [NSMutableArray array];
    while ([rs next])
    {
        YNManyPeopleChat * manyPeopleChat = [[YNManyPeopleChat alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [manyPeopleChat setValuesForKeysWithDictionary:[rs resultDictionary]];
            [twoPeopleChatByKeywordArray addObject:manyPeopleChat];
        }
    }
    [rs close];
    
    return twoPeopleChatByKeywordArray;
}

/**
 修改艾特@数据为默认数据0
 2018-11-16 JC添加
 @param groupType
 @param groupNo
 @param db db
 */
+ (BOOL)updateManyPeopleChatAitWithGroupTyoe:(int)groupType groupNo:(NSString *)groupNo inDatabase:(FMDatabase *)db
{
    NSString *updateManyPeopleChatSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = %@ WHERE %@ = '%@'",
                                         [YNManyPeopleChat tableName:groupType],
                                         [YNManyPeopleChat aitContactMark],
                                         [NSNumber numberWithInt:0],
                                         [YNManyPeopleChat groupNo],
                                         groupNo];
    
    BOOL result = [db executeUpdate:updateManyPeopleChatSql];
    return  result;
}



//更新会话
+ (BOOL)updateManyPeopleChatTableWithMessageID:(NSString *)messageID sendTime:(NSString *)sendTime groupType:(int)groupType isSuccess:(int)messageState inDatabase:(FMDatabase *)db
{
    NSString *updateManyPeopleChatSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '%@', %@ = %@ WHERE %@ = '%@'",
                                         [YNManyPeopleChat tableName:groupType],
                                         [YNManyPeopleChat mTime],
                                         sendTime,
                                         [YNManyPeopleChat isSuccess],
                                         [NSNumber numberWithInt:messageState],
                                         [YNManyPeopleChat packetId],
                                         messageID];
    
    BOOL result = [db executeUpdate:updateManyPeopleChatSql];
    return  result;
}

+ (BOOL)updateManyPeopleChatTableWithMessageID:(NSString *)messageID messageHeight:(CGFloat)messageHeight groupType:(int)groupType inDatabase:(FMDatabase *)db
{
    NSString *updateTwoPeopleChatSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = %@ WHERE %@ = '%@'",
                                        [YNManyPeopleChat tableName:groupType],
                                        [YNManyPeopleChat messageHeight],
                                        [NSNumber numberWithFloat:messageHeight],
                                        [YNManyPeopleChat packetId],
                                        messageID];
    
    BOOL result = [db executeUpdate:updateTwoPeopleChatSql];
    
    return  result;
}

/**
 JC修改消息漫游
 
 @param packId id
 @param messageContinuousKey key
 @param db db
 @return 返回结果
 */
+(BOOL)updateManyPeopleMessageContinuousTableWithPackId:(NSString *)packId groupType:(int)groupType messageContinuousKey:(NSString *)messageContinuousKey isMessageWithdraw:(BOOL)isMessageWithdraw inDatabase:(FMDatabase *)db
{
    NSString *updateTwoPeopleChatSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '%@', %@ = %@ ,%@ = %@ WHERE %@ = '%@'",
                                        [YNManyPeopleChat tableName:groupType],
                                        [YNManyPeopleChat messageContinuousKey],
                                        messageContinuousKey,
                                        [YNManyPeopleChat isMessageContinuous],
                                        [NSNumber numberWithInt:YES],
                                        [YNManyPeopleChat isMessageWithdraw],
                                        [NSNumber numberWithBool:isMessageWithdraw],
                                        [YNManyPeopleChat packetId],
                                        packId];
    
    BOOL result = [db executeUpdate:updateTwoPeopleChatSql];
    return  result;
}




/**
 JC修改所有消息漫游信息
 
 @param messageContinuousKey 新的key
 @param oldMessageContinuousKey 旧的key
 @param db db
 @return 返回结果
 */
+(BOOL)updateManyPeopleAllMessageContinuousWithMessageContinuousKey:(NSString *)messageContinuousKey oldMessageContinuousKey:(NSString *)oldMessageContinuousKey groupType:(int)groupType inDatabase:(FMDatabase *)db
{
    NSString *updateTwoPeopleChatSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '%@' WHERE %@ = '%@' AND %@ = %@",
                                        [YNManyPeopleChat tableName:groupType],
                                        [YNManyPeopleChat messageContinuousKey],
                                        messageContinuousKey,
                                        [YNManyPeopleChat messageContinuousKey],
                                        oldMessageContinuousKey,
                                        [YNManyPeopleChat isMessageContinuous],
                                        [NSNumber numberWithInt:YES]];
    
    BOOL result = [db executeUpdate:updateTwoPeopleChatSql];
    return  result;
}

/**
 JC修改群，讨论组，项目组撤回状态
 
 @param packetId 消息id
 @param groupType
 @param db db
 @return
 */
+(BOOL)updateManyPeopleMessageWithdrawWithPacketId:(NSString *)packetId groupType:(int)groupType inDatabase:(FMDatabase *)db
{
    NSString *updateTwoPeopleChatSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '%@' WHERE %@ = '%@'",
                                        [YNManyPeopleChat tableName:groupType],
                                        [YNManyPeopleChat isMessageWithdraw],
                                        [NSNumber numberWithBool:YES],
                                        [YNManyPeopleChat packetId],
                                        packetId];
    
    BOOL result = [db executeUpdate:updateTwoPeopleChatSql];
    return  result;
}


//只更新状态
+ (BOOL)updateMessageSendingStateWithDatabase:(FMDatabase *)db
{
    NSString *updateMessageSendingStateSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = %@ WHERE %@ = %@",
                                              [YNManyPeopleChat tableName:kContact_UserGroupType],
                                              [YNManyPeopleChat isSuccess],
                                              [NSNumber numberWithInt:YNMessageSendStateFailed],
                                              [YNManyPeopleChat isSuccess],
                                              [NSNumber numberWithInt:YNMessageSendStateSending]];
    BOOL result = [db executeUpdate:updateMessageSendingStateSql];
    
    if (result)
    {
        updateMessageSendingStateSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = %@ WHERE %@ = %@",
                                        [YNManyPeopleChat tableName:kContact_DiscussionGropType],
                                        [YNManyPeopleChat isSuccess],
                                        [NSNumber numberWithInt:YNMessageSendStateFailed],
                                        [YNManyPeopleChat isSuccess],
                                        [NSNumber numberWithInt:YNMessageSendStateSending]];
        result = [db executeUpdate:updateMessageSendingStateSql];
        if (result) {
            updateMessageSendingStateSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = %@ WHERE %@ = %@",
                                            [YNManyPeopleChat tableName:kContact_ProjectTeamGropType],
                                            [YNManyPeopleChat isSuccess],
                                            [NSNumber numberWithInt:YNMessageSendStateFailed],
                                            [YNManyPeopleChat isSuccess],
                                            [NSNumber numberWithInt:YNMessageSendStateSending]];
            result = [db executeUpdate:updateMessageSendingStateSql];
        }
        
    }
    return result;
}

//根据消息的ID清除数据库里面对应的消息
+ (BOOL)deleteAMessageFromDatabaseWithMessageID:(NSString *)messageID groupType:(int)groupType inDatabase:(FMDatabase *)db
{
    NSString *deleteMessageSql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = ?",
                                  [YNManyPeopleChat tableName:groupType],
                                  [YNManyPeopleChat packetId]];
    BOOL result = [db executeUpdate:deleteMessageSql,messageID];
    return result;
}

//更新消息的图片名字, 原图路径, 消息高度
+ (BOOL)updateMessageWithMessageID:(NSString *)messageID
                    originPhotoUrl:(NSString *)originUrl
                          fileName:(NSString *)fileName
                     messageHeight:(CGFloat)messageHeight
                         groupType:(int)groupType
                        inDatabase:(FMDatabase *)db
{
    NSString *updateMessage = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '%@', %@ = '%@', %@ = %@ WHERE %@ = '%@'",
                               [YNManyPeopleChat tableName:groupType],
                               [YNManyPeopleChat originPhotoUrl],
                               originUrl,
                               [YNManyPeopleChat fileName],
                               fileName,
                               [YNManyPeopleChat messageHeight],
                               [NSNumber numberWithFloat:messageHeight],
                               [YNManyPeopleChat packetId], messageID];
    BOOL result = [db executeUpdate:updateMessage];
    
    return result;
}

//更改文件状态2
+ (BOOL)updateMessageFileStateWithMessageID:(NSString *)messageID fileState:(NSInteger)fileState groupType:(int)groupType inDataBase:(FMDatabase *)db
{
    NSString *updateMsg = [NSString stringWithFormat:@"UPDATE %@ SET %@ = %@ WHERE %@ = '%@'",
                           [YNManyPeopleChat tableName:groupType],
                           [YNManyPeopleChat fileState],
                           [NSNumber numberWithInteger:fileState],
                           [YNManyPeopleChat packetId],
                           messageID];
    BOOL result = [db executeUpdate:updateMsg];
    
    return result;
}

/**
 *  更改语音状态
 *
 *  @param messageID 消息ID
 *  @param groupType 分组类型
 *  @param db        db
 *
 *  @return 返回参数
 */
+ (BOOL)updateMessageVoiceStateWithMessageID:(NSString *)messageID  groupType:(int)groupType inDataBase:(FMDatabase *)db
{
    NSString *updateMsg = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '1' WHERE %@ = '%@'",
                           [YNManyPeopleChat tableName:groupType],
                           [YNManyPeopleChat isReaded],
                           [YNManyPeopleChat packetId],
                           messageID];
    BOOL result = [db executeUpdate:updateMsg];
    
    return result;
}


@end
